﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// Interaction logic for DeliveryTest.xaml
    /// </summary>
    public partial class DeliveryTestUserControl : UserControl, IPlugin
    {
        private DeliveryTemplateService deliveryTempalteService = ServiceContainer.GetService<DeliveryTemplateService>();

        public DeliveryTestUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "运费计算"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = deliveryTempalteService.GetAll();
                this.cbbDeliveryTemplate.ItemsSource = item.Datas;
                if (item.Total > 0)
                {
                    this.cbbDeliveryTemplate.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCal_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                double weight = 0, scale = 0;
                weight = double.Parse(this.tbWeight.Text.Trim());
                scale=double.Parse(this.tbDeliveryScale.Text.Trim());
                var delivery = this.cbbDeliveryTemplate.SelectedItem as Mengsk.ProxyDelivery.Domain.DeliveryTemplate;
                if (delivery == null)
                {
                    MessageBox.Show("请选择运费模板");
                    return;
                }

                string address = this.tbAddress.Text.Trim();

                double ret = this.deliveryTempalteService.Computedeliverymoneywithscale(weight, delivery.Id, address,scale);
                this.tbResult.Text = ret.ToString("F2");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
